package com.lovely602.langchain4j.persistent.config;


import com.lovely602.langchain4j.persistent.service.PersistentChatAssistant;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author lizhixing
 */
@Configuration
public class LLMConfig {

    @Bean
    public ChatModel chatModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("AI_DASHSCOPE_API_KEY"))
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .modelName("qwen-plus")
                .build();
    }

    @Bean
    public PersistentChatAssistant persistentChatAssistant(ChatModel chatModel, RedisChatMemoryStore redisChatMemoryStore) {

        ChatMemoryProvider chatMemoryProvider = memoryId -> {
            //按照memoryId对应创建了一个chatMemory
            return MessageWindowChatMemory
                    .builder()
                    .id(memoryId)
                    .maxMessages(100)
                    .chatMemoryStore(redisChatMemoryStore)
                    .build();
        };

        return AiServices.builder(PersistentChatAssistant.class)
                .chatModel(chatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .build();
    }


}
